package com.yangmao.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.yangmao.entity.ActivityInfo;
import com.yangmao.entity.HealthEducation;
import com.yangmao.entity.ActivityInfo;
import com.yangmao.service.api.HealthEducationService;
import com.yangmao.service.api.ActivityInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ‘洋毛’
 * @version 1.0
 * @description: TODO
 * @date 2022/1/7 10:19
 */
@Controller
public class HealthEducationController {
    @Autowired
    private HealthEducationService healthEducationService;

    @GetMapping("/health/education/all.json")
    public ModelAndView getAllActivityInfo(@RequestParam(name="page",required = true,defaultValue = "1") Integer page,
                                         @RequestParam(name="size",required = true,defaultValue = "4")Integer size) {
        ModelAndView mv = new ModelAndView();

        List<HealthEducation> healthEducation = healthEducationService.getAllHealthEducation(page,size);

        PageInfo pageInfo = new PageInfo(healthEducation);

        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("health_education_list");

        return mv;
    }

    @RequestMapping("/health/education/edit.html")
    public ModelAndView editActivityInfo(@RequestParam("id") String healthId, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HealthEducation healthEducation = healthEducationService.getHealthEducationById(healthId);
        mv.addObject("health", healthEducation);
        mv.setViewName("health_education_edit");

        return mv;
    }

    @PostMapping("/health/education/update.html")
    public String updateActivityInfo(HealthEducation healthEducation) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String str = simpleDateFormat.format(d); //将日期转换为字符串且格式按照之前制定的

        healthEducation.setUpdate_time(str);
        healthEducationService.updateHealthEducation(healthEducation);
        return "redirect:/health/education/all.json";
    }

    @RequestMapping("/health/education/delete.html")
    public String deleteHealthEducation(@RequestParam("id") String id) {
        healthEducationService.deleteHealthEducation(id);
        return "redirect:/health/education/all.json";
    }

    @PostMapping("/health/education/add.html")
    public String addActivityInfo(HealthEducation healthEducation){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String str = simpleDateFormat.format(d); //将日期转换为字符串且格式按照之前制定的

        healthEducation.setCreate_time(str);
        healthEducation.setUpdate_time(str);

        healthEducationService.addHealthEducation(healthEducation);

        return "redirect:/health/education/all.json";
    }
}
